from flask import request
from SQLConfig import app
from Interface import Interface
from routes.punctuation import getPuncFilePath, getStlFileName
import constants
import os, json

@app.route('/addAlign', methods=['Get', 'POST'])
def addAlign():  # put application's code here
    """
    
    """
    if request.method=='POST':
        patientId=request.json.get("patientId")
        indexAim = request.json.get("indexAim")
        indexIn = request.json.get("indexIn")
    elif request.method=='GET':
        patientId=request.json.get("patientId")
        indexAim = request.json.get("indexAim")
        indexIn = request.json.get("indexIn")

    indexAim=int(indexAim)
    indexIn=int(indexIn)
    patientId=str(patientId)
    # 根据下标找到stl文件名
    stlFileNameAim=getStlFileName(patientId, indexAim)
    stlFileNameIn=getStlFileName(patientId, indexIn)

    # 根据stl文件名找到标点文件名
    pointAim,pointIn=getPuncFilePath(patientId,stlFileNameAim,stlFileNameIn)

    stlFileNameAim=os.path.join(constants.patientBaseUrl, patientId, constants.patientStlUrl,getStlFileName(patientId, indexAim))
    stlFileNameIn=os.path.join(constants.patientBaseUrl, patientId, constants.patientStlUrl,getStlFileName(patientId, indexIn))

    # 降采样
    txtAim, txtIn = Interface.under_sample(constants.size, stlFileNameAim, stlFileNameIn)

    list1=Interface.abs_rotation(pointAim, pointIn)

    list2=Interface.add_align(txtAim, txtIn, constants.iterations, list1)

    return json.dumps({
        "success":True,
        "matrix":list2
    })